package com.internetCafes.spms.web.customer.service.impl.area;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.internetCafes.spms.core.exception.RRException;
import com.internetCafes.spms.core.exception.RRExceptionCodeEnum;
import com.internetCafes.spms.web.customer.common.cont.AreaConst;
import com.internetCafes.spms.web.customer.common.cont.CommonConst;
import com.internetCafes.spms.web.customer.entity.area.AreaManager;
import com.internetCafes.spms.web.customer.mapper.area.AreaManagerMapper;
import com.internetCafes.spms.web.customer.model.area.EntAreaDo;
import com.internetCafes.spms.web.customer.model.area.EntAreaPo;
import com.internetCafes.spms.web.customer.model.area.EntAreaUserConfigReq;
import com.internetCafes.spms.web.customer.service.area.IAreaManagerService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.internetCafes.spms.web.customer.service.area.IAreaService;
import com.sms.common.util.hutool.core.collection.CollectionUtil;
import com.sms.common.util.hutool.core.date.DateTime;
import com.sms.common.util.hutool.core.date.DateUtil;
import com.sms.common.util.hutool.core.util.ObjectUtil;
import com.sms.common.util.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 区域管理人表 服务实现类
 * </p>
 *
 * @author Zj
 * @since 2021-10-18
 */
@Service
public class AreaManagerServiceImpl extends ServiceImpl<AreaManagerMapper, AreaManager> implements IAreaManagerService {

    @Autowired
    private IAreaService areaService;

    @Override
    public boolean deleteManagerConfig(EntAreaUserConfigReq req) {

        LambdaUpdateWrapper<AreaManager> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AreaManager::getCityId, req.getCityId());

        if (CollectionUtil.isNotEmpty(req.getAreaIdList())) {
            updateWrapper.in(AreaManager::getAreaId, req.getAreaIdList());
        }

        updateWrapper.set(AreaManager::getEffectiveFlag, CommonConst.FlagEnum.NOT.getId());
        return update(updateWrapper);
    }

    @Override
    public List<AreaManager> listNoTenantId(Wrapper<AreaManager> queryWrapper) {
        return baseMapper.listNoTenantId(queryWrapper);
    }

    @Override
    public void updateExpiredManager() {
        baseMapper.updateExpiredManager();
    }

    @Override
    public boolean config(EntAreaUserConfigReq req) {
        if (ObjectUtil.isNull(req.getCityId()) || CollectionUtil.isEmpty(req.getUserIdList())
                || ObjectUtil.isNull(req.getTypeId()) || ObjectUtil.isNull(req.getAlwaysFlag())
                || !AreaConst.AreaUserConfigTypeEnum.getTypeIdList().contains(req.getTypeId())) {
            throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
        }
        if (CommonConst.FlagEnum.NOT.getId().equals(req.getAlwaysFlag())) {
            if (StrUtil.isBlank(req.getEndTime())) {
                throw new RRException(RRExceptionCodeEnum.PARAM_ERROR);
            }
        }
        // 已分配的进行覆盖
        deleteManagerConfig(req);

        DateTime now = DateTime.now();

        // 获取区域相关信息
        List<Long> areaIdList;
        if (CollectionUtil.isNotEmpty(req.getAreaIdList())) {
            areaIdList = req.getAreaIdList();
        } else {
            areaIdList = areaService.poList(new EntAreaDo().setParentId(req.getCityId()))
                    .stream()
                    .map(EntAreaPo::getId)
                    .collect(Collectors.toList());
            areaIdList.add(req.getCityId());
        }
        DateTime endTime = null;
        if (StrUtil.isNotBlank(req.getEndTime())) {
            endTime = DateUtil.parseDate(req.getEndTime());
        }

        DateTime finalEndTime = endTime;
        List<AreaManager> areaManagerBatchDo = areaIdList
                .stream()
                .map(areaId ->  new AreaManager()
                        .setCityId(req.getCityId())
                        .setAreaId(areaId)
                        .setStartTime(now)
                        .setEffectiveFlag(CommonConst.FlagEnum.IS.getId())
                        .setManagerId(CollectionUtil.getFirst(req.getUserIdList()))
                        .setEndTime(finalEndTime)
                        .setAlwaysFlag(req.getAlwaysFlag()))
                .collect(Collectors.toList());

        return saveBatch(areaManagerBatchDo);
    }
}
